﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class Form1 : Form
    {
        public UserControl1 UserControl1 { get; set; }
        public UserControl2 UserControl2 { get; set; }
        public UserControl3 UserControl3 { get; set; }


        public Form1(UserControl1 userControl1, UserControl2 userControl2, UserControl3 userControl3)
        {
            InitializeComponent();

            //方法一：通过new方式添加，但是每次回new新对象，数据回丢失
            //panel1.Controls.Add(new UserControl1());

            //方法二：IOC通过构造函数注入
            UserControl1 = userControl1;
            UserControl2 = userControl2;
            UserControl3 = userControl3;

            //方法三：通过IOC容器直接注入
            var userControl = Program.ServiceProvider.GetRequiredService<UserControl1>();
            panel1.Controls.Add(userControl);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            //userControl.Dock = DockStyle.Fill;

        }

        private void btn_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();

            //panel1.Controls.Add(new UserControl1());

            //panel1.Controls.Add(UserControl1);

            var userControl = Program.ServiceProvider.GetRequiredService<UserControl1>();
            panel1.Controls.Add(userControl);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();

            //panel1.Controls.Add(new UserControl2());

            //panel1.Controls.Add(UserControl2);

            var userControl = Program.ServiceProvider.GetRequiredService<UserControl2>();
            panel1.Controls.Add(userControl);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();

            //panel1.Controls.Add(new UserControl3());

            //panel1.Controls.Add(UserControl3);

            var userControl = Program.ServiceProvider.GetRequiredService<UserControl3>();
            panel1.Controls.Add(userControl);
        }
    }
}
